package hua.rqtandrsp;

import com.ibm.mq.MQC;
import com.ibm.mq.MQEnvironment;
import com.ibm.mq.MQException;
import com.ibm.mq.MQGetMessageOptions;
import com.ibm.mq.MQMessage;
import com.ibm.mq.MQPutMessageOptions;
import com.ibm.mq.MQQueue;
import com.ibm.mq.MQQueueManager;

/**
 * 获取数据的时候是
 * @author HuangHua
 *
 */
public class Requester {
	@SuppressWarnings( { "unchecked", "deprecation" })
	public static void main(String args[]) {
		try {
			String hostName = "192.168.17.183";
			String channel = "SYSTEM.ADMIN.SVRCONN";
			String qManager = "QM_CRM_TEST";
//			String qName = "QU_FOSS_ORDER_001_IN";
			int port = 1414;
			String userID = "mqm";
			String password = "mqm";

			String requestQueue = "QU_FOSS_ORDER_001_IN";
			String replyToQueue = "QU_FOSS_ORDER_001_IN_BK";
			String replyToQueueManager = "QM_CRM_TEST";

			/* 设置MQEnvironment 属性以便客户机连接 */
			MQEnvironment.hostname = hostName;
			MQEnvironment.channel = channel;
			// MQEnvironment.properties.put(MQC.TRANSPORT_PROPERTY,MQC.TRANSPORT_WEBSPHERE
			// MQ);
			MQEnvironment.properties.put(MQC.TRANSPORT_PROPERTY,
					MQC.TRANSPORT_MQSERIES_CLIENT);
			MQEnvironment.port = port;
			MQEnvironment.userID = userID;
			MQEnvironment.password = password;

			/* 连接到队列管理器 */
			MQQueueManager qMgr = new MQQueueManager(qManager);

			/* 设置打开选项以便打开用于输出的队列，如果队列管理器停止，我们也已设置了选项去应对不成功情况 */
			int openOptions = MQC.MQOO_OUTPUT | MQC.MQOO_FAIL_IF_QUIESCING;

			/* 打开打开队列 */
			MQQueue queue = qMgr.accessQueue(requestQueue, openOptions, null,
					null, null);

			/* 设置放置消息选项，我们将使用默认设置 */
			MQPutMessageOptions pmo = new MQPutMessageOptions();
			pmo.options = pmo.options + MQC.MQPMO_NEW_MSG_ID;
			pmo.options = pmo.options + MQC.MQPMO_SYNCPOINT;

			/* 创建消息缓冲区 */
			MQMessage outMsg = new MQMessage();

			/* 设置MQMD 格式字段 */
			outMsg.format = MQC.MQFMT_STRING;
			outMsg.messageFlags = MQC.MQMT_REQUEST;
			outMsg.replyToQueueName = replyToQueue;
			outMsg.replyToQueueManagerName = replyToQueueManager;

			/* 准备用户数据消息 */
			String msgString = "Test Request Message from Requester program, test for requester by HuangHua... ";
			outMsg.writeString(msgString);

			/* 在队列上放置消息 */
			queue.put(outMsg, pmo);

			/* 提交事务 */
			qMgr.commit();

			System.out.println(" The message has been Successfully put\n");

			/* 关闭请求队列 */
			queue.close();

			/* 设置打开选项以便队列响应 */
			openOptions = MQC.MQOO_INPUT_SHARED | MQC.MQOO_FAIL_IF_QUIESCING;
			MQQueue respQueue = qMgr.accessQueue(replyToQueue, openOptions,
					null, null, null);
			MQMessage respMessage = new MQMessage();
			MQGetMessageOptions gmo = new MQGetMessageOptions();

			/* 在同步点控制下获取消息 */
			gmo.options = gmo.options + MQC.MQGMO_SYNCPOINT;
			gmo.options = gmo.options + MQC.MQGMO_WAIT;
			gmo.matchOptions = MQC.MQMO_MATCH_CORREL_ID;
			gmo.waitInterval = 20000;
			respMessage.correlationId = outMsg.messageId;

			/* 获取响应消息 */
			respQueue.get(respMessage, gmo);
			String responsetest = respMessage.readLine();
			System.out.println(responsetest);
			String response = respMessage.readString(respMessage
					.getMessageLength());
			
			System.out.println("The response message is : " + response);
			qMgr.commit();
			respQueue.close();
			qMgr.disconnect();
		} catch (MQException ex) {
			System.out.println("An MQ Error Occurred: Completion Code is :\t"
					+ ex.completionCode + "\n\n The Reason Code is :\t"
					+ ex.reasonCode);
			ex.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
